<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : product_brand.class.php
 * ....商品品牌操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Sat Mar 28 10:45:59 CST 2009
 */

class ProductBrandClass extends FrameWork {
	/**
	 * 取品牌列表
	 * @author workflow Develop Team     
	 * @param  $condition 检索条件
	 * @param  $obj_page 分页类
	 * @return array
	 */
	function getProductBrand($condition,&$obj_page){
		$conditionstr = $this->_conditionSearch($condition);
		$brand_list = $GLOBALS['db']->GetList($conditionstr, $obj_page, "product_brand");
		return $brand_list;
	}
	
	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  $condition 检索条件
	 * @return sting
	 */
	function _conditionSearch($condition){
		$condition_str = '';
		
		//父ID
		if ($condition['pb_u_id'] != ''){
			$condition_str .= " and pb_u_id = '" . $condition['pb_u_id'] . "'";
		}
		//pb_id in
		if ($condition['pb_id_in'] != '') {
			$condition_str .= " and pb_id in (". $condition['pb_id_in'] .")";
		}
		//pb_id
		if ($condition['pb_id'] != '') {
			$condition_str .= " and pb_id = '". $condition['pb_id_in'] . "'";
		}
		//pb_id like
		if ($condition['pb_path_like'] != '') {
			$condition_str .= " and pb_path like '%".$condition['pb_path_like']."%'";
		}
		//状态
		if ($condition['pb_state'] != ''){
			$condition_str .= " and pb_state = '" . $condition['pb_state'] . "'";
		}else if ($condition['pb_state'] != 'none'){
			//
		}else {
			$condition_str .= " and pb_state = '0'";
		}
		//排序
		if ($condition['order_by'] != ''){
			$condition_str .= " order by " . $condition['order_by'] . "";
		}
		
		return $condition_str;
	}
	
	/**
	 * 重新生成品牌分类
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function restartProductBrand(){
		require_once("fileoperate.class.php");
		//地区列表
		$condition['order_by'] = 'pb_deeppath asc,pb_u_id asc,pb_sort asc,pb_id asc ';
		$ar = $this->getProductBrand($condition,$page);
		//定义目标数组
		$d = array();
		//定义索引数组，用于记录节点在目标数组的位置
		$ind = array();
		foreach($ar as $v) {
			$v['child'] = array(); //给每个节点附加一个child项
			if($v['pb_u_id'] == 0) {
				$i = count($d);
				$d[$i] = $v;
				$ind[$v['pb_id']] =& $d[$i];
			}else {
				$i = count($ind[$v['pb_u_id']]['child']);
				$ind[$v['pb_u_id']]['child'][$i] = $v;
				$ind[$v['pb_id']] =& $ind[$v['pb_u_id']]['child'][$i];
			}
		}
		//生成一维数组，用于后台新增和修改页面的下拉列表
		$result = array();
		$string = $this->getOneLevelArray($result,$d,array(0=>'pb_id',1=>'pb_u_id',2=>'pb_name',3=>'pb_sort',4=>'pb_deeppath'));
		$file_string = $this->getOneLevelString($string);
		$creatfile = "../cache/product_brand.php";
		$make_p_class_single = FileOperate::makeProductBrandFlie($file_string,$creatfile);
		if ($make_p_class_single !== true) {
			return $make_p_class_single;
		}
		return true;
	}
	
	/**
	 * 生成品牌的一维数组
	 * @author     
	 * @param  
	 * @return array
	 */
	function getOneLevelArray(&$result, $node, $Fields){
		if (is_array($node)) {
			foreach ($node as $k => $v){
				$num = count($result);
				$result[$num][0] = $v[$Fields[0]];
				$result[$num][1] = $v[$Fields[1]];
				$result[$num][2] = Common::magic_value($v[$Fields[2]]);
				$result[$num][3] = $v[$Fields[3]];
				$result[$num][4] = $v[$Fields[4]];
				if (is_array($v['child'])){
					$result = $this->getOneLevelArray($result, $v['child'], $Fields);
				}
				unset($num);
			}
		}
		return $result;
	}
	
	/**
	 * 把一维数组拼成字符串
	 * 
	 * @author     
	 * @param  
	 * @return string
	 */
	function getOneLevelString($param){
		if (is_array($param)) {
			foreach ($param as $k => $v){
				$file_string .="array('".$v[0]."','".$v[1]."','".$v[2]."','".$v[3]."','".$v[4]."',";
				foreach ($param as $k2 => $v2){
					$i = 0;
					if ($v[0] == $v2[1]){
						$i = 1;
						break;
					}
				}
				//haveson 有下级目录的标识，1为有，0为没有
				if ($i == 1){
					$file_string .="1";
				}else {
					$file_string .="0";
				}
				$file_string .="),\n";
			}
		}
		return $file_string;
	}
	
	/**
	 * 添加商品品牌
	 * @author workflow Develop Team     
	 * @param  $input_param
	 * @return bool
	 */
	function addProductBrand($input_param){
		$valueArray = array();
		$valueArray['pb_name'] = str_replace("，",",",trim($input_param['txtBrandName']));
		$valueArray['pb_u_id'] = $input_param['txtParentBrand']?$input_param['txtParentBrand']:'0';
		$valueArray['pb_sort'] = $input_param['txtBrandSort'];
		if ($valueArray['pb_u_id'] > 0){
			$brand_array = $this->getProductBrandRow($valueArray['pb_u_id']);
			$valueArray['pb_deeppath'] = $brand_array['pb_deeppath']+1;
			unset($brand_array);
			$id = $valueArray['pb_u_id'];
			$vali_array = array();
			$vali_array[0] = '0';
			for ($i=2;;$i++){//如果有父ID 那么深度最少就为2了
				$brand_array = $this->getProductBrandRow($id);
				$vali_array[] = $brand_array['pb_id'];
				if ($brand_array['pb_u_id'] == '0'){
					break;
				}else {
					$id = $brand_array['pb_u_id'];
				}
			}
			//去除重复数据
			$vali_array = array_unique($vali_array);
			//排序
			asort($vali_array);
			//拼成字符串
			$valueArray['pb_path'] = ','.implode(',',$vali_array).',';
			unset($id,$vali_array);
		}else {
			$valueArray['pb_path'] = ','.'0'.',';
			$valueArray['pb_deeppath'] = 1;//深度
		}
		$valueArray['pb_image'] = $input_param['brand_image'];
		$result = $GLOBALS['db']->InsertRow($valueArray, 'product_brand', 'pb_id');
		return $result;
	}
	
	/**
	 * 取品牌内容
	 * @author workflow Develop Team     
	 * @param  $pb_id
	 * @return array
	 */
	function getProductBrandRow($pb_id){
		if (intval($pb_id) > 0){
			return $GLOBALS['db']->GetOneRow($pb_id,'product_brand','pb_id');
		}
		return false;
	}
	
	/**
	 * 更新品牌
	 *
	 * @param array $input
	 * @return bool
	 */
	function updateProductBrand($input_param){
		//语言包
		$this->getlang('sys_product_brand.manage');
		$valueArray = array();
		$valueArray['pb_id'] = $input_param['id'];
		$valueArray['pb_name'] = str_replace("，",",",trim($input_param['txtBrandName']));
		$valueArray['pb_u_id'] = $input_param['txtParentBrand']?$input_param['txtParentBrand']:'0';
		$valueArray['pb_sort'] = $input_param['txtBrandSort'];
		if ($valueArray['pb_u_id'] > 0){
			$brand_array = $this->getProductBrandRow($valueArray['pb_u_id']);
			$valueArray['pb_deeppath'] = $brand_array['pb_deeppath']+1;
			unset($brand_array);
			$id = $valueArray['pb_u_id'];
			$vali_array = array();
			$vali_array[0] = '0';
			for ($i=1;;$i++){
				$brand_array = $this->getProductBrandRow($id);
				$vali_array[] = $brand_array['pb_id'];
				if ($brand_array['pb_u_id'] == '0'){
					break;
				}else {
					$id = $brand_array['pb_u_id'];
				}
			}
			//去除重复数据
			$vali_array = array_unique($vali_array);
			//排序
			@asort($vali_array);
			//拼接后并校验父串中是否已经存在本ID，用来防止选择本或下级类
			if ( in_array($valueArray['pb_id'],$vali_array)){
				echo  "{type:0,message:'". $this->_lang['errSysProductBrandMergerSame'] ."'}";exit;
			}
			//拼成字符串
			$valueArray['pb_path'] = ','.implode(',',$vali_array).',';
			unset($id,$vali_array);
		}else {
			$valueArray['pb_path'] = ','.'0'.',';
			$valueArray['pb_deeppath'] = 1;//深度
		}
		$valueArray['pb_image'] = $input_param['brand_image'];
		if ($GLOBALS['db']->UpdateRow($valueArray['pb_id'],$valueArray, 'product_brand', 'pb_id')){
			$result = $this->restartProductBrand();
			if($result !== true){
				return false;
			}
			$this->reCreateProductBrandPath();
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 重新生成全部PATH
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function reCreateProductBrandPath(){
		$brand_list = $GLOBALS['db']->GetAllRow("product_brand","*"," order by pb_id asc ");
		foreach ($brand_list as $k=>$v){
			$update_array = array();
			if(is_array($v)){
				if (intval($v['pb_u_id'])>0){
					$parent_row = $this->getProductBrandRow(intval($v['pb_u_id']));
					$update_array['pb_path'] = ','.trim($parent_row['pb_path'],',').",".$parent_row['pb_id'].',';
				}else{
					$update_array['pb_path'] = ','.'0'.',';
				}
				$GLOBALS['db']->UpdateRow(intval($v['pb_id']),$update_array,"product_brand","pb_id");
			}
		}
	}
	
	/**
	 * 将旧的父ID更新为新的
	 * @author workflow Develop Team     
	 * @param  old_parent_id 旧的父ID，new_parent_id 新的父ID
	 * @return bool
	 */
	function updateProductBrandByParentId($param){
		$value_array = array();
		$value_array['pb_u_id'] = $param['new_parent_id'];
		$result = $GLOBALS['db']->UpdateRow(intval($param['old_parent_id']),$value_array, 'product_brand', 'pb_u_id');
		return $result;
	}
	
	/**
	 * 删除品牌
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function delProductBrand($pb_id){
		if (intval($pb_id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($pb_id),'product_brand','pb_id');
		}else {
			return false;
		}
		
		$this->reCreateProductBrandPath();
		$this->restartProductBrand();//重新生成数组文件
		return $result;
	}
	
	/**
	 * 更新排序
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function updateProductBrandSort($SortArray){
		$value_array = array();
		$value_array['pb_sort'] = $SortArray['txtBrandSort'];
		$result = $GLOBALS['db']->UpdateRow(intval($SortArray['txtPbId']),$value_array, 'product_brand', 'pb_id');
		if ($result === true){
			return true;
		}
		return false;
	}
	
	/**
	 * 取品牌 所属的各级品牌
	 * @author workflow Develop Team     
	 * @param  $pb_id
	 * @return bool/array
	 */
	function getProductBrandPathList($pb_id){
		if (intval($pb_id) > 0){
			$brand_array = $this->getProductBrandRow(intval($pb_id));
			if ($brand_array['pb_u_id'] != '0'){
				$condition_brand['pb_id_in'] = trim($brand_array['pb_path'],',');
				$brand_list = $this->getProductBrand($condition_brand,$page);
				$brand_list[] = $brand_array;
			}else {
				$brand_list[] = $brand_array;
			}
			return $brand_list;
		}else {
			return false;
		}
	}
	
	/**
	 * 更新品牌深度
	 * @author workflow Develop Team     
	 * @param  
	 * @return bool
	 */
	function updateProductBrandDeepPath($classID,$level = 0){
		$parent_array = $GLOBALS['db']->GetAllRow("product_brand","pb_id,pb_u_id,pb_deeppath"," where pb_u_id='" . $classID . "'");
		if (count($parent_array) > 0){
			foreach($parent_array as $k=>$v){
				$value_array = array();
				$value_array['pb_deeppath'] = $level+1;
				$result = $GLOBALS['db']->UpdateRow($v['pb_id'],$value_array,'product_brand','pb_id');
				$this->updateProductBrandDeepPath($v['pb_id'],$level+1);
			}
		}
		return true;
	}
	
	/**
	 * 批量更新父ID
	 * @author workflow Develop Team     
	 * @param  $id_array 转移的id $parent_id父id
	 * @return bool
	 */
	function updateProductBrandToMove($id_array,$parent_id){
		if (intval($parent_id) > 0 && !empty($id_array)){
			foreach ($id_array as $k => $v){
				$condition_str[] = "pb_id = '" . $v . "'";
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['pb_u_id'] = intval($parent_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('product_brand',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
		
	}
}
?>